<template>
  <a-select
    v-model="curValue"
    :placeholder="enumData.Name"
    @select="handleSelected"
    @change="changeHandler"
    v-bind="$attrs"
  >
    <a-select-option v-for="item in enumItems" :key="item.Value" :value="item.Value">{{ item.Name }}</a-select-option>
  </a-select>
</template>
<script>
export default {
  props: {
    IsPeriodOfValidity: { type: Boolean, required: false, default: true },
    code: { type: String, required: true },
    type: { type: String, required: false },
    value: { type: String, required: false }
  },
  data() {
    return {
      curValue: '',
      enumData: {},
      enumItems: []
    }
  },
  watch: {
    code(code) {
      this.getEnum()
    },
    value(value) {
      this.curValue = value
    }
  },
  mounted() {
    this.curValue = this.value
    this.getEnum()
  },
  methods: {
    getEnum() {
      console.log(this.type)
      var storageKey = 'Enum_' + this.code
      var enumJson
      this.$http.get('/Base/Base_Enum/GetByCode?code=' + this.code + '&type=' + this.type).then(resJson => {
        this.enumData = resJson.Data
        this.enumItems = [...resJson.Data.EnumItems]
        console.log(this.IsPeriodOfValidity)
        if (this.IsPeriodOfValidity == false) {
          this.enumItems = this.enumItems.filter(item => {
            return item.Code != 'LastDateFO'
          })
        }

        console.log(this.enumItems)
        this.$emit('getlist', this.enumItems)
        enumJson = JSON.stringify(resJson.Data)

        localStorage.setItem(storageKey, enumJson)
      })
    },
    handleSelected(val) {
      this.$emit('input', val)
    },
    changeHandler(a) {
      console.log(a)
      this.$emit('onchange', this.curValue)
    }
  }
}
</script>
